#!/usr/bin/env python
import dieu.util, dieu.gui.add, dieu.gui.prefs, dieu.gui.about, dieu.gui.browser
import webbrowser
import pysqlite2
import gtk
import os
import sys
import pynotify
import time

class DieuTrayIcon:
    def __init__(self):
        self.trayicon = gtk.StatusIcon()
        self.trayicon.set_from_file('@prefix@/share/pixmaps/dieu/dieu.svg')
        self.trayicon.set_tooltip('dieu Bookmarks')
        self.trayicon.connect('activate', lambda w: dieu.gui.add.DieuAddPost())
        self.trayicon.connect('popup-menu', self.on_right_click)
        
    def on_right_click(self, status_icon, button, activate_time):
        for user in dieu.settings.get_users():
            globals()['self.database_'+user] = dieu.database.DieuDatabase(user.split(':')[0], user.split(':')[1])

        self.right_click_menu = gtk.Menu()
        
        self.add_post = gtk.ImageMenuItem('Add post')
        self.add_post_image = gtk.Image()
        self.add_post_image.set_from_icon_name('add', gtk.ICON_SIZE_MENU)
        self.add_post.set_image(self.add_post_image)
        self.add_post.connect('activate', lambda w: dieu.gui.add.DieuAddPost())
        self.add_post.show()
        self.right_click_menu.append(self.add_post)
        
        if dieu.settings.get_show_all() == True:
            for user in dieu.settings.get_users():
                globals()['self.username_submenu_'+user] = gtk.ImageMenuItem(user.split(':')[0])
                if user.split(':')[1] == 'del.icio.us':
                    globals()['self.username_submenu_image_'+user] = gtk.Image()
                    globals()['self.username_submenu_image_'+user].set_from_file('@prefix@/share/pixmaps/dieu/delicious.png')
                    globals()['self.username_submenu_'+user].set_image(globals()['self.username_submenu_image_'+user])
                elif user.split(':')[1] == 'ma.gnolia':
                    globals()['self.username_submenu_image_'+user] = gtk.Image()
                    globals()['self.username_submenu_image_'+user].set_from_file('@prefix@/share/pixmaps/dieu/magnolia.png')
                    globals()['self.username_submenu_'+user].set_image(globals()['self.username_submenu_image_'+user])
                elif user.split(':')[1] == 'bluedot':
                    globals()['self.username_submenu_image_'+user] = gtk.Image()
                    globals()['self.username_submenu_image_'+user].set_from_file('@prefix@/share/pixmaps/dieu/bluedot.ico')
                    globals()['self.username_submenu_'+user].set_image(globals()['self.username_submenu_image_'+user])
                globals()['self.username_submenu_'+user+'_tags_menu'] = gtk.Menu()
                globals()['self.username_submenu_'+user].set_submenu(globals()['self.username_submenu_'+user+'_tags_menu'])
                
                try: globals()['self.database_'+user].get_all_tags('Default'); self.database_works = True
                except KeyError:
                    self.database_works = False
                
                if self.database_works == True:
                    for tag in globals()['self.database_'+user].get_all_tags('Alphabetical'):
                        if tag != '':
                            globals()['self.user_submenu_'+user+'_tags_menu_tag_'+tag[1]+'_image'] = gtk.Image()
                            globals()['self.user_submenu_'+user+'_tags_menu_tag_'+tag[1]] = gtk.ImageMenuItem(tag[1][:100])
                            globals()['self.user_submenu_'+user+'_tags_menu_tag_'+tag[1]+'_menu'] = gtk.Menu()
                            globals()['self.user_submenu_'+user+'_tags_menu_tag_'+tag[1]].set_submenu(globals()['self.user_submenu_'+user+'_tags_menu_tag_'+tag[1]+'_menu'])
                            globals()['self.user_submenu_'+user+'_tags_menu_tag_'+tag[1]].set_image(globals()['self.user_submenu_'+user+'_tags_menu_tag_'+tag[1]+'_image'])
                            globals()['self.user_submenu_'+user+'_tags_menu_tag_'+tag[1]].show()            
                            globals()['self.username_submenu_'+user+'_tags_menu'].append(globals()['self.user_submenu_'+user+'_tags_menu_tag_'+tag[1]])

                    for post in globals()['self.database_'+user].get_all_posts('Default'):
                        def make_bookmark_click(post):
                            def on_bookmark_click(self):
                                webbrowser.open(post[2])
                            return on_bookmark_click

                    for post in globals()['self.database_'+user].get_all_posts('Default'):
                        for tag in post[3].split(' '):
                            globals()['self.menuitem_'+str(post[0])+post[2]] = gtk.ImageMenuItem(post[1][:100])
                            globals()['self.menuitem_'+str(post[0])+post[2]].connect('activate', make_bookmark_click(post))
                            globals()['self.menuitem_'+str(post[0])+post[2]].show()
                            globals()['self.user_submenu_'+user+'_tags_menu_tag_'+tag+'_menu'].append(globals()['self.menuitem_'+str(post[0])+post[2]])
                    
                    globals()['self.username_submenu_'+user].show()
                    self.right_click_menu.append(globals()['self.username_submenu_'+user])
                elif self.database_works == False:
                    globals()['self.username_error_'+user] = gtk.ImageMenuItem('Database error in '+user+' database')
                    globals()['self.username_submenu_'+user+'_tags_menu'].append(globals()['self.username_error_'+user])
                    globals()['self.username_error_'+user].show()
                    globals()['self.username_submenu_'+user].show()
                    self.right_click_menu.append(globals()['self.username_submenu_'+user])
        else:
            for user in dieu.settings.get_users():
                globals()['self.username_name_'+user] = user.split(':')[0]
                globals()['self.username_pass_'+user] = user.split(':')[1]
                globals()['self.username_submenu_'+user] = gtk.ImageMenuItem(user.split(':')[0])
                if user.split(':')[1] == 'del.icio.us':
                    globals()['self.username_submenu_image_'+user] = gtk.Image()
                    globals()['self.username_submenu_image_'+user].set_from_file('@prefix@/share/pixmaps/dieu/delicious.png')
                    globals()['self.username_submenu_'+user].set_image(globals()['self.username_submenu_image_'+user])
                elif user.split(':')[1] == 'ma.gnolia':
                    globals()['self.username_submenu_image_'+user] = gtk.Image()
                    globals()['self.username_submenu_image_'+user].set_from_file('@prefix@/share/pixmaps/dieu/magnolia.png')
                    globals()['self.username_submenu_'+user].set_image(globals()['self.username_submenu_image_'+user])
                elif user.split(':')[1] == 'bluedot':
                    globals()['self.username_submenu_image_'+user] = gtk.Image()
                    globals()['self.username_submenu_image_'+user].set_from_file('@prefix@/share/pixmaps/dieu/bluedot.ico')
                    globals()['self.username_submenu_'+user].set_image(globals()['self.username_submenu_image_'+user])
                globals()['self.username_submenu_'+user].connect('activate', lambda w: dieu.gui.browser.DieuBrowser(globals()['self.username_name_'+user], globals()['self.username_pass_'+user]))
                globals()['self.username_submenu_'+user].show()
                self.right_click_menu.append(globals()['self.username_submenu_'+user])
            
        separator = gtk.SeparatorMenuItem()
        separator.show()
        self.right_click_menu.append(separator)
        
        if dieu.settings.get_recent() == True:
            for user in dieu.settings.get_users():
                try: globals()['self.database_'+user].get_all_tags('Default'); self.database_works = True
                except pysqlite2.dbapi2.OperationalError:
                    self.database_works = False    
        
                if self.database_works == True:
                    nameitem = gtk.ImageMenuItem(user.split(':')[0])
                    if user.split(':')[1] == 'del.icio.us':
                        globals()['nameitemimage_'+user] = gtk.Image()
                        globals()['nameitemimage_'+user].set_from_file('@prefix@/share/pixmaps/dieu/delicious.png')
                    elif user.split(':')[1] == 'ma.gnolia':
                        globals()['nameitemimage_'+user] = gtk.Image()
                        globals()['nameitemimage_'+user].set_from_file('@prefix@/share/pixmaps/dieu/magnolia.png')
                    elif user.split(':')[1] == 'bluedot':
                        globals()['nameitemimage_'+user] = gtk.Image()
                        globals()['nameitemimage_'+user].set_from_file('@prefix@/share/pixmaps/dieu/bluedot.ico')
                    nameitem.set_image(globals()['nameitemimage_'+user])
                    nameitem.set_sensitive(False)
                    self.right_click_menu.append(nameitem)
                    nameitem.show()
                    
                    for post in globals()['self.database_'+user].get_all_posts('Default'):
                        def make_bookmark_click(post):
                            def on_bookmark_click(self):
                                webbrowser.open(post[2])
                            return on_bookmark_click
                    
                    for post in globals()['self.database_'+user].get_recent_posts('Default', 5):
                        globals()['self.recent_menuitem_'+str(post[0])+post[2]] = gtk.ImageMenuItem(post[1][:100])
                        globals()['self.recent_menuitem_'+str(post[0])+post[2]].connect('activate', make_bookmark_click(post))
                        globals()['self.recent_menuitem_'+str(post[0])+post[2]].show()
                        self.right_click_menu.append(globals()['self.recent_menuitem_'+str(post[0])+post[2]])
                
                if self.database_works == False:
                    nameitem = gtk.ImageMenuItem(user.split(':')[0])
                    if user.split(':')[1] == 'del.icio.us':
                        globals()['nameitemimage_'+user] = gtk.Image()
                        globals()['nameitemimage_'+user].set_from_file('@prefix@/share/pixmaps/dieu/delicious.png')
                    elif user.split(':')[1] == 'ma.gnolia':
                        globals()['nameitemimage_'+user] = gtk.Image()
                        globals()['nameitemimage_'+user].set_from_file('@prefix@/share/pixmaps/dieu/magnolia.png')
                    elif user.split(':')[1] == 'bluedot':
                        globals()['nameitemimage_'+user] = gtk.Image()
                        globals()['nameitemimage_'+user].set_from_file('@prefix@/share/pixmaps/dieu/bluedot.ico')
                    nameitem.set_image(globals()['nameitemimage_'+user])
                    nameitem.set_sensitive(False)
                    self.right_click_menu.append(nameitem)
                    nameitem.show()
                    
                    globals()['error_item_'+user] = gtk.ImageMenuItem('Database error in '+user+' database')
                    self.right_click_menu.append(globals()['error_item_'+user])
                    globals()['error_item_'+user].show()
                    
            self.separator = gtk.SeparatorMenuItem()
            self.separator.show()
            self.right_click_menu.append(self.separator)
    
        self.update = gtk.ImageMenuItem('Refresh database')
        self.update_image = gtk.Image()
        self.update_image.set_from_stock(gtk.STOCK_REFRESH, gtk.ICON_SIZE_MENU)
        self.update.set_image(self.update_image)
        self.update.connect('activate', lambda w: dieu.util.sync())
        self.update.show()
        self.right_click_menu.append(self.update    )
        
        self.preferences = gtk.ImageMenuItem('Preferences')
        self.preferences_image = gtk.Image()
        self.preferences_image.set_from_stock(gtk.STOCK_PREFERENCES, gtk.ICON_SIZE_MENU)
        self.preferences.set_image(self.preferences_image)
        self.preferences.connect('activate', lambda w: dieu.gui.prefs.DieuPreferencesWindow())
        self.preferences.show()
        self.right_click_menu.append(self.preferences)
        
        self.about = gtk.ImageMenuItem('About')
        self.about_image = gtk.Image()
        self.about_image.set_from_stock(gtk.STOCK_ABOUT, gtk.ICON_SIZE_MENU)
        self.about.set_image(self.about_image)
        self.about.connect('activate', lambda w: dieu.gui.about.DieuAboutDialog())
        self.about.show()
        self.right_click_menu.append(self.about)
        
        self.exit = gtk.ImageMenuItem('Quit')
        self.exit_image = gtk.Image()
        self.exit_image.set_from_stock(gtk.STOCK_QUIT, gtk.ICON_SIZE_MENU)
        self.exit.set_image(self.exit_image)
        self.exit.connect('activate', lambda w: sys.exit())
        self.exit.show()
        self.right_click_menu.append(self.exit)
        
        self.right_click_menu.popup(None, None, gtk.status_icon_position_menu, button, activate_time, status_icon)

